package com.yckj.dao;

import com.yckj.domain.User;
import com.yckj.util.DbUtilHikari;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.hutool.crypto.SecureUtil;

/**
 * 对用户表的操作：注册用户、删除用户、查询用户、修改用户
 * @author yuzehao
 * @version 1.0
 */
public class UserDao {

    /**
     * 查询所有用户
     * @return
     */
    public List<User> selectalluser(){
        List<User> userList=new ArrayList<User>();
        Connection conn =null;
        PreparedStatement pstmt=null;
        ResultSet rs=null;

        conn=DbUtilHikari.getConnection();
        String sql="SELECT userid,username,userpsw FROM users";
        try {
            pstmt =conn.prepareStatement(sql);
            rs=pstmt.executeQuery();
            while (rs.next()){
                User user=new User();
                user.setUserid(rs.getInt("userid"));
                user.setUsername(rs.getString("username"));
                user.setUserpsw(rs.getString("userpsw"));
                userList.add(user);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DbUtilHikari.close(rs,pstmt,conn);
        }

        return userList;
    }

    /**
     * 用户更新
     * @param userid
     * @param uname
     * @param upsw
     * @return
     */

    public int updateuser(int userid,String uname,String upsw){
        int flag=0;
        Connection conn=null;
        PreparedStatement pstmt=null;
        conn =DbUtilHikari.getConnection();
        String sql="UPDATE users SET username=?,userpsw=? WHERE userid=?";
        try {
            pstmt=conn.prepareStatement(sql);
            pstmt.setString(1,uname);
            pstmt.setString(2,SecureUtil.md5(upsw));
            pstmt.setInt(3,userid);
            flag=pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DbUtilHikari.close(pstmt,conn);
        }
        return flag;
    }

    /**
     * 按照ID用户删除
     * @param userid
     * @return
     */
    public int deleteuser(int userid){
        int flag=0;
        Connection conn =null;
        PreparedStatement pstmt=null;
        conn=DbUtilHikari.getConnection();
        String sql="DELETE FROM users WHERE userid=?";
        try {
            pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1,userid);
            flag=pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DbUtilHikari.close(pstmt,conn);
        }

        return flag;
    }
    /**
     * 用户登录
     * @param uname
     * @param upsw
     * @return
     */
    public boolean login(String uname,String upsw){
        //flag表示登录成功与否，true成功，false失败
        boolean flag = false;
        // conn代表数据库连接
        Connection conn = null;
        // 预编译对象，用pstmt执行SQL语句
        PreparedStatement pstmt = null;
        //rs代表结果集，从数据库表中查到的结果放在rs中
        ResultSet rs = null;
        //  获取连接
        conn = DbUtilHikari.getConnection();
        String sql = "SELECT * FROM users WHERE username=? AND userpsw=?";
        try {
            pstmt = conn.prepareStatement(sql);
            //给？赋值
            pstmt.setString(1,uname);
            pstmt.setString(2,SecureUtil.md5(upsw));

            // 执行SQL语句（下达执行指令）
            rs = pstmt.executeQuery();
            //判断rs结果里有没有数据，next（）判断rs里是否有数据
            if (rs.next()){
                flag = true;
            }else {
                flag = false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
        }
        return flag;
    }


    /**
     * 用户注册
     * @param uname 用户名
     * @param upsw 密码
     * @return 注册成功返回1，否则返回0
     */
    public int insertUser(String uname,String upsw) {
        // 1. 声明一些变量备用
        // conn代表数据库连接
        Connection conn = null;
        // 预编译对象，用pstmt执行SQL语句
        PreparedStatement pstmt = null;
        // 注册成功与否的标志，如果是0注册失败；如果1注册成功
        int flag = 0;
        // 2. 获取连接
        conn = DbUtilHikari.getConnection();
        // 3. 定义并准备SQL语句
        String sql = "INSERT INTO users(username,userpsw) VALUES(?,?)";
        try {
            pstmt = conn.prepareStatement(sql);
            //4. 给?赋值
            pstmt.setString(1,uname);
            pstmt.setString(2,SecureUtil.md5(upsw));
            // 5. 执行SQL语句（下达执行指令）
            flag = pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            // 6. 关闭数据库资源
            DbUtilHikari.close(pstmt,conn);
        }
        return flag;
    }
}